//
//  View.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/4/26.
//

import Foundation

public extension UIView {
    
    // MARK: - frame操作
    /// frame bounds
    var y: CGFloat {
       get {
           return frame.origin.y
       }
       set {
           frame.origin.y = newValue
       }
   }
    
    var x: CGFloat {
       get {
           return frame.origin.x
       }
       set {
           frame.origin.x = newValue
       }
   }
    
    var width: CGFloat {
       get {
           return frame.size.width
       }
       set {
           frame.size.width = newValue
       }
   }
    
    var height: CGFloat {
       get {
           return frame.size.height
       }
       set {
           frame.size.height = newValue
       }
   }
    
    var size: CGSize {
       get {
           return frame.size
       }
       set {
           width = newValue.width
           height = newValue.height
       }
   }
    
    // MARK: - view所在的controller
     /// view所在的controller
    var parentViewController: UIViewController? {
        weak var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
    
    // MARK: - view所在第一响应者
    /// 第一响应者
    func firstResponder() -> UIView? {
       var views = [UIView](arrayLiteral: self)
       var i = 0
       repeat {
           let view = views[i]
           if view.isFirstResponder {
               return view
           }
           views.append(contentsOf: view.subviews)
           i += 1
       } while i < views.count
       return nil
    }
    
    // MARK: - 添加移除视图
    /// 添加多个子视图
    func addSubviews(_ subviews: [UIView]) {
        subviews.forEach({ self.addSubview($0) })
    }
    
    /// 移除多个子视图
    func removeSubviews() {
        subviews.forEach({ $0.removeFromSuperview() })
    }
    
    // MARK: - 添加圆角
    /// 添加4个不同大小的圆角
    func addCorner(_ cornerRadii: CornerRadii){
        let path = createPathWithRoundedRect(bounds: self.bounds, cornerRadii:cornerRadii)
        let shapLayer = CAShapeLayer()
        shapLayer.frame = self.bounds
        shapLayer.path = path
        self.layer.mask = shapLayer
    }
    
    func addCorner(_ cornerRadii: CornerRadii, layerRect rect: CGRect) {
        let path = createPathWithRoundedRect(bounds: rect, cornerRadii:cornerRadii)
        let shapLayer = CAShapeLayer()
        shapLayer.frame = rect
        shapLayer.path = path
        self.layer.mask = shapLayer
    }
    
    ///各圆角大小
    struct CornerRadii {
        var topLeft: CGFloat = 0
        var topRight: CGFloat = 0
        var bottomLeft: CGFloat = 0
        var bottomRight: CGFloat = 0
        public init(topLeft: CGFloat = 0, topRight: CGFloat = 0, bottomLeft: CGFloat = 0, bottomRight: CGFloat = 0) {
            self.topLeft = topLeft
            self.topRight = topRight
            self.bottomLeft = bottomLeft
            self.bottomRight = bottomRight
        }
        public init(_ all: CGFloat) {
            self.init(topLeft: all, topRight: all, bottomLeft: all, bottomRight: all)
        }
    }
    
    /// 切圆角函数绘制线条
    func createPathWithRoundedRect(bounds: CGRect, cornerRadii: CornerRadii) -> CGPath {
        let minX = bounds.minX
        let minY = bounds.minY
        let maxX = bounds.maxX
        let maxY = bounds.maxY
      
        //获取四个圆心
        let topLeftCenterX = minX +  cornerRadii.topLeft
        let topLeftCenterY = minY + cornerRadii.topLeft

        let topRightCenterX = maxX - cornerRadii.topRight
        let topRightCenterY = minY + cornerRadii.topRight

        let bottomLeftCenterX = minX +  cornerRadii.bottomLeft
        let bottomLeftCenterY = maxY - cornerRadii.bottomLeft

        let bottomRightCenterX = maxX -  cornerRadii.bottomRight
        let bottomRightCenterY = maxY - cornerRadii.bottomRight

        //虽然顺时针参数是YES，在iOS中的UIView中，这里实际是逆时针
        let path :CGMutablePath = CGMutablePath();
        //顶 左
        path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: false)
        //顶右
        path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: CGFloat.pi * 3 / 2, endAngle: 0, clockwise: false)
        //底右
        path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: false)
        //底左
        path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: false)
        path.closeSubpath();
        return path;
    }
    
    
    // MARK: - 单个边框
    /// 添加边框
    func addBorderWithColor(color: UIColor, rect: CGRect) {
        self.layer.addSublayer(calayer(color: color, rect: rect))
     }
//
//    func addTopBorderWithColor(color: UIColor, width: CGFloat) {
//        self.layer.addSublayer(calayer(color: color, rect: CGRect(0, 0, self.frame.size.width, width)))
//     }
//
//     func addRightBorderWithColor(color: UIColor, width: CGFloat) {
//        self.layer.addSublayer(calayer(color: color, rect: CGRect(self.frame.size.width - width, 0, width, self.frame.size.height)))
//     }
//
//     func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
//        self.layer.addSublayer(calayer(color: color, rect: CGRect(0, self.frame.size.height - width, self.frame.size.width, width)))
//     }
//
//     func addLeftBorderWithColor(color: UIColor, width: CGFloat) {
//        self.layer.addSublayer(calayer(color: color, rect: CGRect(0, 0, width, self.frame.size.height)))
//     }

    private func calayer(color: UIColor, rect: CGRect) -> CALayer {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = rect
        return border
    }

    /// 单个边框
    enum side:String {
      case top
      case bottom
      case left
      case right
    }
    func addBorder(side:side, color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        switch side {
        case .top:
          border.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: width)
        case .bottom:
          border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
        case .left:
          border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        case .right:
          border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        }
        self.layer.addSublayer(border)
    }
    func addBorder(color: UIColor, layerRect rect: CGRect) {
        let border = CALayer()
        border.backgroundColor = color.cgColor
        border.frame = rect;
        self.layer.addSublayer(border)
    }
    
    //MARK: - 截图
    /// 将当前视图转为UIImage
    func screenshot() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
    
}

